package com.ruoyi.bzjgl.controller;

import com.ruoyi.bzjgl.domain.Bzjtable;
import com.ruoyi.bzjgl.domain.FileTable;
import com.ruoyi.bzjgl.service.IBackToPersonService;
import com.ruoyi.bzjgl.service.IBzjtableService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/confiscate/backToPerson")
public class BackToPersonController extends BaseController {

    @Autowired
    private IBackToPersonService backToPersonService;
    @Autowired
    private IBzjtableService bzjtableService;

    /**
     * 查询退回查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = backToPersonService.selectThglList(bzjtable);
        return getDataTable(list);
    }


    @GetMapping("/{xh}")
    public AjaxResult getInfo(@PathVariable("xh") String xh)
    {
        return success(backToPersonService.selectThByXh(xh));
    }

    @GetMapping("/thshList")
    public TableDataInfo thshList(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = backToPersonService.selectThshList(bzjtable);
        return getDataTable(list);
    }

    @GetMapping("/thhzList")
    public TableDataInfo thhzList(Bzjtable bzjtable)
    {
        List<String> depNames = bzjtableService.getDepList();
        bzjtable.setDepNames(depNames);
        startPage();
        List<Bzjtable> list = backToPersonService.selectThhzList(bzjtable);
        return getDataTable(list);
    }

    //没收附件上传地址
    @PostMapping("/uploadThFile")
    public AjaxResult uploadThFile(@RequestParam("file") MultipartFile file) throws Exception{
        try{
            // 上传文件路径
            String fileFirstPath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileLastPath = FileUploadUtils.upload(fileFirstPath, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION).replace("/upload","");
            AjaxResult ajax = AjaxResult.success();
            HashMap<String,Object> fileTable = new HashMap<>();
            fileTable.put("fileFirstPath", fileFirstPath);
            fileTable.put("fileLastPath", fileLastPath);
            fileTable.put("newFileName", FileUtils.getName(fileLastPath));
            fileTable.put("originalFilename", file.getOriginalFilename());
            List<HashMap<String,Object>> fileTables = new ArrayList<>();
            fileTables.add(fileTable);
            ajax.put("fileTables",fileTables);
            return ajax;
        }catch (Exception e){
            return   AjaxResult.error(e.getMessage());
        }
    }

    //提交审核
    @PostMapping("/updateThshzt")
    public AjaxResult updateThshzt(@RequestBody Bzjtable bzjtable)
    {
        return  toAjax(backToPersonService.updateThshzt(bzjtable));
    }

    @PostMapping("/thsh")
    public AjaxResult thsh(@RequestBody Bzjtable bzjtable)
    {
        return  toAjax(backToPersonService.thsh(bzjtable));
    }

    @PostMapping("/shBack")
    public AjaxResult shBack(@RequestBody Bzjtable bzjtable)
    {
        return  toAjax(backToPersonService.shBack(bzjtable));
    }
}
